<!doctype html>
<html lang="zh-CN">
<head>
    <title>我的邀请</title>
    <script src="./rem.js"></script>
    <link rel="stylesheet" href="./my-invite.css"/>
    <meta charset="UTF-8">
</head>
<body>
<div class="container" style="overflow: hidden">
    <p class="h-center money-title title">累计分润(元)</p>
    <p class="h-center money" id="money">0.00</p>
    <p class="h-center title fans-title">粉丝数</p>
    <p class="h-center fans-num" id="fans-num">0个</p>
    <div class="invite-frame" id="copy">
        <span class="invite-text invite-left">我的邀请码(点击复制)</span>
        <span class="invite-text invite-right" id="invite-code"></span>
    </div>
    <p class="p-level">今日会员等级:<span id="customer-level"></span></p>
    <div class="desc">
        <div class="tab">
            <span id="allDesc" class="tab-text active">累计详情</span>
            <span id="todayDesc" class="tab-text disable">今日详情</span>
        </div>
        <div class="desc-title"> 
            <span id="fans" class="desc-title-text">活跃粉丝</span>
            <span id="shareMoney" class="desc-title-text">分润金额</span>
        </div>
        <div id="list"></div>
    </div>
</div>
<script>
    var vCopy = document.getElementById("copy");
    var vAllDesc = document.getElementById("allDesc");
    var vTodayDesv = document.getElementById("todayDesc");
    var vInviteCode = document.getElementById("invite-code");
    var vMoney = document.getElementById("money");
    var vFansNum = document.getElementById("fans-num");
    var vCustomerLevel = document.getElementById("customer-level");
    var vList = document.getElementById("list");
    var vFans = document.getElementById("fans");

    var allDescData = [];
    var todayDescData = [];

    try {
        window.wallet.getInviteCode();
    } catch (error) {
        console.log(error);
    }

    vCopy.onclick = function (e) {
        window.wallet.copy(vInviteCode.innerText);
    };
    vAllDesc.onclick = function(e) {
        showAllDesc();
    };
    vTodayDesv.onclick = function(e) {
        showTodayDesc();
    };

    function setData(jsonStr){
        var json = JSON.parse(jsonStr);
        vInviteCode.innerText = json.invite_code;
        vMoney.innerText = json.total_profit;
        vFansNum.innerText = json.fans_count;

        var levelStr = "无";
        switch(json.grade){
            case 1 : levelStr = "普通会员";
                break;
            case 2 : levelStr = "业务组长";
                break;
            case 3 : levelStr = "业务经理";
                break;
            case 4 : levelStr = "业务总监";
                break;
        }
        vCustomerLevel.innerText = levelStr;
        allDescData = json.profit_list;
        todayDescData = json.today_profit_list;
        
        var list = [];
        if(vAllDesc.classList.contains("active")){
            list = allDescData;
        } else{
            list = todayDescData;
        }
        vList.innerHTML = getListHtmlStr(list);
    }

    function showAllDesc(){
        if(!vAllDesc.classList.contains("active")){
            vFans.innerText="累计粉丝";
            vAllDesc.classList.add("active");
            vAllDesc.classList.remove("disable");
            vTodayDesv.classList.add("disable");
            vTodayDesv.classList.remove("active");
            vList.innerHTML = getListHtmlStr(allDescData);
        }
    }

    function showTodayDesc(){
        if(!vTodayDesv.classList.contains("active")){
            vFans.innerText="活跃粉丝";
            vTodayDesv.classList.add("active");
            vTodayDesv.classList.remove("disable");
            vAllDesc.classList.add("disable");
            vAllDesc.classList.remove("active")
            vList.innerHTML = getListHtmlStr(todayDescData);
        }
    }

    function getListHtmlStr(list){
        var inner = "";
        for (const i in list) {
            inner += `<div class="desc-item"><span class="desc-item-text" >${list[i].count}人</span><span class="desc-item-text" >${list[i].money}元</span></div>`;
        }
        return inner;
    }
</script>
</body>
</html>